package com.nageoffer.shortlink.admin.remote.dto;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nageoffer.shortlink.admin.common.convention.result.Result;
import com.nageoffer.shortlink.admin.remote.dto.Req.ShortLinkCreateReqDTO;
import com.nageoffer.shortlink.admin.remote.dto.Req.ShortLinkPageReqDTO;
import com.nageoffer.shortlink.admin.remote.dto.Req.ShortLinkUpdateReqDTO;
import com.nageoffer.shortlink.admin.remote.dto.Resp.ShortLinkCreateRespDTO;
import com.nageoffer.shortlink.admin.remote.dto.Resp.ShortLinkGroupCountRespDTO;
import com.nageoffer.shortlink.admin.remote.dto.Resp.ShortLinkPageRespDTO;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public interface ShortLinkRemoteService {

    default Result<ShortLinkCreateRespDTO> createShortLink( ShortLinkCreateReqDTO requestParam){
        Map<String,Object> requestMap = new HashMap<>();
        requestMap.put("domain",requestParam.getDomain());
        requestMap.put("originUrl",requestParam.getOriginUrl());
        requestMap.put("gid",requestParam.getGid());
        requestMap.put("createType",requestParam.getCreatedType());
        requestMap.put("validDateType",requestParam.getValidDateType());
        requestMap.put("validDate",requestParam.getValidDate());
        requestMap.put("describe",requestParam.getDescribe());
        requestMap.put("favicon",requestParam.getFavicon());
        HttpRequest request = HttpUtil.createPost("http://127.0.0.1:8001/api/short-link/project/v1/shortlink/create")
                .header("Content-Type", "application/json")
                .body(JSON.toJSONString(requestMap));
        String resultCreateStr = request.execute().body();
        return JSON.parseObject(resultCreateStr, new TypeReference<Result<ShortLinkCreateRespDTO>>() {
        });
    }
    /**
     * 后管调用中台的分页查看短链接
     * @param requestParam
     * @return
     */
    default Result<IPage<ShortLinkPageRespDTO>> pageShortLink( ShortLinkPageReqDTO requestParam){
        Map<String , Object> requestMap = new HashMap<>();
        requestMap.put("gid",requestParam.getGid());
        requestMap.put("pageNum",requestParam.getPageNum());
        requestMap.put("pageSize",requestParam.getPageSize());
        HttpRequest request = HttpUtil.createPost("http://127.0.0.1:8001/api/short-link/project/v1/shortlink/page")
                .header("Content-Type", "application/json")
                .body(JSON.toJSONString(requestMap));
        String resultPageStr = request.execute().body();
        return JSON.parseObject(resultPageStr, new TypeReference<Result<IPage<ShortLinkPageRespDTO>>>() {
        });
    }
    /**
     * 后管调用中台的分组查询短链接数目接口
     */
    default Result<List<ShortLinkGroupCountRespDTO>> listGroupShortLinkCount(@RequestParam("gidList") List<String> requestParam){
        Map<String , Object> requestMap = new HashMap<>();
        requestMap.put("gidList",requestParam);
        String resultCountStr = HttpUtil.get("http://127.0.0.1:8001/api/short-link/project/v1/shortlink/count",requestMap);
        return JSON.parseObject(resultCountStr, new TypeReference<Result<List<ShortLinkGroupCountRespDTO>>>() {
        });
    }

    /**
     * 后管调用中台的更新短链接功能，但是功能实现不完全
     * @param requestParam
     */
    default void updateShortLink(ShortLinkUpdateReqDTO requestParam){
        String body = HttpUtil.createRequest(Method.PUT, "http://127.0.0.1:8001/api/short-link/project/v1/shortlink/update")
                .header("Content-Type", "application/json")
                .body(JSON.toJSONString(requestParam))
                .execute()
                .body();
        System.out.println(body);

    }
}
